from django.db import models
from location.models import Street
from tagstar.db.fields import TagsField


PROPERTY_TYPE_HOUSE = 0
PROPERTY_TYPE_UNIT = 1
PROPERTY_TYPE_BUSINESS = 2
PROPERTY_TYPE_COMMERCIAL = 3
PROPERTY_TYPE_COMMUNITY = 4
PROPERTY_TYPE_FARM = 5
PROPERTY_TYPE_FLATS = 6
PROPERTY_TYPE_LAND = 7
PROPERTY_TYPE_OTHER = 8


class BaseProperty(models.Model):

    PROPERTY_TYPE = (
        (PROPERTY_TYPE_HOUSE, 'house'), (PROPERTY_TYPE_UNIT, 'unit'), (PROPERTY_TYPE_BUSINESS, 'business'),
        (PROPERTY_TYPE_COMMERCIAL, 'commercial'), (PROPERTY_TYPE_COMMUNITY, 'community'), (PROPERTY_TYPE_FARM, 'farm'),
        (PROPERTY_TYPE_FLATS, 'flats'), (PROPERTY_TYPE_LAND, 'land'), (PROPERTY_TYPE_OTHER, 'other'))

    BEDROOMS = (
        (0, 'No Bedroom'), (1, 'One Bedroom'), (2, 'Two Bedrooms'), (3, 'Three Bedrooms'),
        (4, 'Four Bedrooms'), (5, 'Five Bedrooms'), (6, 'More than Six'))

    BATHROOMS = (
        (0, 'No Bathroom'), (1, 'One Bathroom'), (2, 'Two Bathrooms'), (3, 'Three Bathrooms'),
        (4, 'Four Bathrooms'), (5, 'More Than Five'))

    ENSUITE = ((0, 'No Ensuite'), (1, 'One Ensuite'), (2, 'Two Ensuites'), (3, 'Three Ensuites'),(4, 'More Than Four'))

    CAR_SPACE = ((0, 'No Garage'), (1, 'One Garage'), (2, 'Double Garage'), (3, 'Triple Garage'), (4, 'More Than Four'))

    USAGE = ((0, 'Residential'), (1, 'Commercial'), (2, 'Business'), (4, 'Industrial'), (5, 'Farm'))

    ZONE = ((0, 'Residential A'), (1, 'Residential B'), (2, 'Residential C'),
            (3, 'Commercial A'), (4, 'Commercial B'), (5, 'Commercial C'),
            (6, 'Industrial A'), (7, 'Industrial B'), (8, 'Industrial C'))

    street = models.ForeignKey(Street)
    street_number = models.CharField(max_length=20)
    slug = models.SlugField(max_length=255, unique=True)
    type = models.SmallIntegerField(choices=PROPERTY_TYPE)

    bedroom = models.SmallIntegerField(choices=BEDROOMS)
    ensuite = models.SmallIntegerField(choices=ENSUITE)
    bathroom = models.SmallIntegerField(choices=BATHROOMS)
    car_space = models.SmallIntegerField(choices=CAR_SPACE)

    land_area = models.IntegerField(max_length=50)
    floor_area = models.IntegerField(max_length=50)

    primary_use = models.SmallIntegerField(choices=USAGE)
    zone_code = models.SmallIntegerField(choices=ZONE)
    lot_plan = models.CharField(max_length=255)
    lga = models.CharField(max_length=255)

    class Meta:
        abstract = True

class Property(BaseProperty):

    latitude = models.CharField(max_length=255, null=False)
    longitude = models.CharField(max_length=255, null=False)

    tags = TagsField()

    class Meta(BaseProperty.Meta):
        db_table = "real_estate_property"
        ordering = ['street', 'street_number']
        verbose_name_plural = 'Properties'

    def __unicode__(self):
        return self.street_number + self.street.name

    @models.permalink
    def get_absolute_url(self):
        return ('detail', (), { 'property_slug': self.slug })


class Transfer(BaseProperty):

    TRANSFER_TYPE = ((0, 'Auction'), (1, 'Normal Sale'))
    SALE_TYPE = ((0, 'Auction'), (1, 'Normal Sale'))

    contract_date = models.DateField()
    possession_date = models.DateField()
    transfer_price = models.DecimalField(max_digits=9, decimal_places=2)
    transfer_type = models.SmallIntegerField(choices=TRANSFER_TYPE)

    is_agents_advice = models.BooleanField(default=False)
    agent_name = models.CharField(max_length=255)
    agency_name = models.CharField(max_length=255)

    sale_type= models.SmallIntegerField(choices=SALE_TYPE)
    capital_value_at_sale = models.DecimalField(max_digits=9, decimal_places=2)
    ucv_at_sale = models.DecimalField(max_digits=9, decimal_places=2)
    valuation_date_at_sale = models.DateField()
    improvements_value = models.DecimalField(max_digits=9, decimal_places=2)

    document_number = models.CharField(max_length=255)

    class Meta(BaseProperty.Meta):
        db_table = "real_estate_transfer"
        ordering = ["-contract_date", "-possession_date", "transfer_price", "transfer_type"]
        verbose_name_plural = "Sales"

    def __unicode__(self):
        return "_Sold_For_" +self.transfer_price

class Rental(BaseProperty):

    PERIOD_TYPE = ((0, 'Three Months'), (1, 'Six Months'), (2, 'One Year'))

    percent_first_to_last_list_price = models.DecimalField(max_digits=9, decimal_places=2)
    days_on_the_market = models.IntegerField()
    start_rent_date = models.DateField()
    latest_rent_date = models.DateField()
    first_advertised_date = models.DateField()
    latest_rent_description = models.CharField(max_length=255)
    last_advertised_date = models.DateField()
    agency_name = models.CharField(max_length=255)
    agent_name = models.CharField(max_length=255)

    period_code = models.SmallIntegerField(choices=PERIOD_TYPE)
    daily_rent = models.DecimalField(max_digits=9, decimal_places=2)

    class Meta(BaseProperty.Meta):
        db_table = "real_estate_rental"
        ordering = ['-last_advertised_date', '-latest_rent_date', 'daily_rent']
        verbose_name_plural = "Rental"

    def __unicode__(self):
        return self.property + "_For_Rent_" +self.daily_rent

class Listing(BaseProperty):

    days_on_the_market = models.SmallIntegerField()
    listing_start_date = models.DateField()
    start_listing_price = models.DecimalField(max_digits=9, decimal_places=2)
    current_listing_price = models.DecimalField(max_digits=9, decimal_places=2)
    current_price_description = models.CharField(max_length=255)
    agency_name = models.CharField(max_length=255)
    agent_name = models.CharField(max_length=255)
    current_listing_date = models.DateField()
    current_listing_type = models.CharField(max_length=50)

    class Meta(BaseProperty.Meta):
        db_table = "real_estate_listing"
        ordering = ['-current_listing_date', '-start_listing_price', 'current_listing_price']
        verbose_name_plural = "On The Market"

    def __unicode__(self):
        return self.property + "_Listing_For" +self.current_listing_price







